﻿/// <summary>
/// 功能：
/// 作者：chao.jiang
/// 日期：2012/9/29 11:55:16
/// 备注：
/// 修改：
/// </summary>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Phoenix.Service.Model;

namespace Phoenix.Service
{
    public class PagerService
    {
        /// <summary>
        /// 获取分页模型
        /// </summary>
        /// <param name="pagesize">一页显示多少条</param>
        /// <param name="pageindex">当前页</param>
        /// <param name="recordqty">记录总数</param>
        /// <returns></returns>
        public static PagerModel GetPagerModel(int pageSize, int pageIndex, int recordQty)
        {
            PagerModel m = new PagerModel();

            double pageqty = Math.Ceiling((double)recordQty / pageSize);
            m.PageQty = (int)pageqty;

            m.PageIndex = pageIndex == 0 ? 1 : pageIndex;

            m.PreviousPageIndex = (pageIndex > 1 ? pageIndex - 1 : 1);
            m.NextPageIdex = (pageIndex < m.PageQty ? pageIndex + 1 : m.PageQty);

            m.FirstPageIdex = 1;
            m.LastPageIndex = m.PageQty;

            m.PageSize = pageSize;
            m.RecordQty = recordQty;

            m.BeginPageIndex = pageSize * (pageIndex - 1) + 1;
            m.EndPageIndex = pageSize * pageIndex;

            return m;
        }

        /// <summary>
        /// 获取分页模型
        /// </summary>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <param name="recordQty"></param>
        /// <param name="ppindex">页数的当前页</param>
        /// <param name="ppSize">页数的页面大小</param>
        /// <returns></returns>
        public static PagerModel ProcessPagerModel(int pageSize, string pageIndex, int recordQty, string ppindex, int ppSize)
        {
            if (string.IsNullOrEmpty(pageIndex) || pageIndex == "0")
                pageIndex = "1";
            int pindex = int.Parse(pageIndex);
            //记录的分页模型
            PagerModel recordMod = Service.PagerService.GetPagerModel(pageSize, pindex, recordQty);

            //页数的分页模型
            int p1index = (int)Math.Ceiling((double)pindex / ppSize);
            PagerModel pageMod = Service.PagerService.GetPagerModel(ppSize, p1index, recordMod.PageQty);
            recordMod.PageQtyPagerModel = pageMod;

            return recordMod;
        }

        public static PagerModel ProcessPagerModel(int pageSize, string pageIndex, int recordQty, int ppSize)
        {
            return ProcessPagerModel(pageSize, pageIndex, recordQty, "", ppSize);
        }

        public static PagerModel ProcessPagerModel(int pageIndex, int recordQty)
        {
            return ProcessPagerModel(ParamConstant.PAGESIZE, pageIndex.ToString(), recordQty, ParamConstant.PAGESIZE);
        }
    }

}
